﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PlayerStat
{
    public class ScrapeHelper
    {
        /// <summary>
        /// Trim the input string, and if it is null, return string.Empty
        /// </summary>
        /// <param name="str">input string</param>
        /// <returns>output string</returns>
        public static string GetSafeString(string str)
        {
            return ((string.IsNullOrWhiteSpace(str)) ? string.Empty : str.Trim());
        }

        /// <summary>
        /// Get the match URL for the all round of specified season
        /// </summary>
        /// <param name="year">season's start year</param>
        /// <param name="leagueID">the specified leagueID</param>
        /// <returns>URL</returns>
        public static string GetAllMatchURL(int year, int leagueID)
        {
            //logger.Debug("#GetAllMatchURL# Starts. [PARAM IN]Year:" + year.ToString());

            string strURL = string.Empty;
            switch (leagueID)
            {
                case 12: // Norway 1st league
                    {
                        if (2006 == year) strURL = @"http://www.fotball.no/System-pages/TabellTermin/?tournamentId=82890";
                        else if (2007 == year) strURL = @"http://www.fotball.no/System-pages/TabellTermin/?tournamentId=82864";
                        else if (2008 == year) strURL = @"http://www.fotball.no/System-pages/TabellTermin/?tournamentId=82849";
                        else if (2009 == year) strURL = @"http://www.fotball.no/System-pages/TabellTermin/?tournamentId=37562";
                        else if (2010 == year) strURL = @"http://www.fotball.no/System-pages/TabellTermin/?tournamentId=81239";
                        else if (2011 == year) strURL = @"http://www.fotball.no/System-pages/TabellTermin/?tournamentId=125844";
                        break;
                    }
                case 26: // Norway 2nd league
                    {
                        if (2006 == year) strURL = @"http://www.fotball.no/System-pages/TabellTermin/?tournamentId=82891";
                        else if (2007 == year) strURL = @"http://www.fotball.no/System-pages/TabellTermin/?tournamentId=82865";
                        else if (2008 == year) strURL = @"http://www.fotball.no/System-pages/TabellTermin/?tournamentId=82850";
                        else if (2009 == year) strURL = @"http://www.fotball.no/System-pages/TabellTermin/?tournamentId=37588";
                        else if (2010 == year) strURL = @"http://www.fotball.no/System-pages/TabellTermin/?tournamentId=81230";
                        else if (2011 == year) strURL = @"http://www.fotball.no/System-pages/TabellTermin/?tournamentId=125931";
                        break;
                    }
                case 13: // Sweden 1st league
                    {
                        if (2006 == year) strURL = @"http://svenskfotboll.se/allsvenskan/tidigare-ar/resultat-2006/tabell/?scr=fixturelist&ftid=516";
                        else if (2007 == year) strURL = @"http://svenskfotboll.se/allsvenskan/tidigare-ar/resultat-2007/tabell/?scr=fixturelist&ftid=641";
                        else if (2008 == year) strURL = @"http://svenskfotboll.se/allsvenskan/tidigare-ar/resultat-2008/tabell-resultat/?scr=fixturelist&ftid=14999";
                        else if (2009 == year) strURL = @"http://svenskfotboll.se/allsvenskan/tidigare-ar/thylins-allsvenska-summering-2009/tabell-och-resultat/?scr=fixturelist&ftid=19440";
                        else if (2010 == year) strURL = @"http://svenskfotboll.se/allsvenskan/tidigare-ar/resultat-2010/tabell-och-resultat/?scr=fixturelist&ftid=27018";
                        else if (2011 == year) strURL = @"http://svenskfotboll.se/allsvenskan/tidigare-ar/resultat-2011/tabell-och-resultat/?scr=fixturelist&ftid=31625";
                        break;
                    }
                case 16: // Denmark 1st League, TODO
                    {
                        if (2005 == year) strURL = @"http://www.ligafodbold.dk/?cat=statsResults&cat_id=0&tID=3";
                        else if (2006 == year) strURL = @"http://www.ligafodbold.dk/?cat=statsResults&cat_id=0&tID=2";
                        else if (2007 == year) strURL = @"http://www.ligafodbold.dk/?cat=statsResults&cat_id=0&tID=1";
                        else if (2008 == year) strURL = @"http://www.ligafodbold.dk/?cat=statsResults&cat_id=0&tID=107";
                        else if (2009 == year) strURL = @"http://www.ligafodbold.dk/?cat=statsResults&cat_id=0&tID=130";
                        else if (2010 == year) strURL = @"http://www.ligafodbold.dk/?cat=statsResults&cat_id=0&tID=136";
                        else if (2011 == year) strURL = @"http://www.ligafodbold.dk/?cat=statsResultsSeason";
                        break;
                    }
                case 18: // Switland 1st league
                    {
                        if (2011 == year) strURL = @"http://www.football.ch/sfl/de/Ranglisten-Resultate-ASL.aspx";
                        else strURL = @"http://www.football.ch/sfl/de/arc_rera.aspx";

                        break;
                    }
                case 27: // Switland 2nd league
                    {
                        if (2011 == year) strURL = @"http://www.football.ch/sfl/de/Ranglisten-Resultate-ChL.aspx";
                        else strURL = @"http://www.football.ch/sfl/de/arc_rera.aspx";

                        break;
                    }
                default:
                    {
                        throw new Exception("Wrong League Number");
                    }
            }

            //logger.Debug("#GetAllMatchURL# Ends. [PARAM OUT] URL:" + strURL);

            return strURL;
        }

        /// <summary>
        /// Get the playerNumber
        /// </summary>
        /// <param name="tempNumber">input tempNumber</param>
        /// <returns>PlayerNumber</returns>  
        public static int GenPlayerNumber(int tempNumber)
        {
            return (tempNumber % 30);
        }

        /// <summary>
        /// Get the playerID
        /// </summary>
        /// <param name="str">the player's url, which is provided by Web Owner</param>
        /// <param name="year">The specified season, different season may have different team account</param>
        /// <param name="round">The specified round</param>
        /// <returns>PlayerID</returns>       
        public static int GetPlayerID(string str, int leagueID)
        {         
            try
            {
                switch (leagueID)
                {
                    case 12:
                    case 26:
                        {
                            string temp = str.Remove(0, @"http://www.fotball.no/Community/Profil/?fiksId=".Length);
                            return int.Parse(temp);
                        }
                    case 16:
                        {
                            // sample: http://www.ligafodbold.dk/?cat=statsPlayerView&cat_id=1&pID=580&tID=18
                            string tempOri = str.Remove(0, @"http://www.ligafodbold.dk/?cat=statsPlayerView&cat_id=1&pID=".Length);
                            string temp = tempOri.Remove(tempOri.IndexOf("&tID="));
                            return int.Parse(temp);
                        }
                    default:
                        {
                            return -1;
                        }
                }
                
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public void DownloadErrorPages(int leagueid)
        {
            System.Net.WebClient wc = new System.Net.WebClient();
            DBUtil util = new DBUtil();
            var results = from t in util.dbClass.tblScrapes
                          where t.leagueid == leagueid
                          select t;
            log4net.ILog logger = log4net.LogManager.GetLogger("scrape");
            while (true)
            {
                try
                {
                    foreach (tblScrape tbl in results)
                    {
                        wc.DownloadFile(tbl.stat_url1, string.Format(@"C:\Scrape\Error\{0}\{1}.html", leagueid, tbl.matchid));
                        logger.DebugFormat("Download match by {0}", tbl.matchid);
                    }
                    break;
                }
                catch (Exception ex)
                {
                }
            }
            wc.Dispose();
        }
    }
}
